⚡️ Speed up method EntityV3DatastoreSpec.additional_properties_type by 18%
#13
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 18% (0.18x) speedup for
EntityV3DatastoreSpec.additional_properties_typeinsrc/datadog_api_client/v2/model/entity_v3_datastore_spec.py⏱️ Runtime :
2.35 microseconds→1.98 microsecondss(best of30runs)📝 Explanation and details
The optimized code achieves an 18% speedup through two key micro-optimizations in the
__init__method:1. Local variable caching for
unset: The optimization storesunsetas a local variable_unsetto avoid repeated global lookups. In Python, local variable access is significantly faster than global/module-level lookups since locals are stored in an array and accessed by index rather than through dictionary lookups.2. Batched dictionary updates: Instead of performing individual
kwargs["key"] = valueassignments (which requires separate dictionary hash lookups and insertions), the code collects all assignments in a temporaryargmapdictionary and performs a singlekwargs.update(argmap)operation. This reduces the number of dictionary operations from potentially 4 separate insertions to 1 batch update.The optimizations are most effective when multiple parameters are provided (as shown in the test cases), since:
unsetlookup savings multiply with each parameter checkif component_of is not _unset or ...can short-circuit early if no parameters are setThese micro-optimizations preserve all original behavior while reducing the overhead of object initialization, which is particularly valuable for data models that may be instantiated frequently in API client scenarios.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-EntityV3DatastoreSpec.additional_properties_type-mgcajzdwand push.